<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\mall\controller;

use app\common\controller\Frontend;


class MallHome extends Frontend {
	protected $noNeedLogin = ['*'];
	protected $noNeedRight = ['*'];

	protected $goodsModel;
	protected $categoryModel;
	protected $orderModel;

	protected function initialize()
	{
		parent::initialize();


		$this->assignCategory();

		$this->setItem();

	}

	private function setItem()
	{
		$order_count  = model('mall/mallOrder')->where(['user_id' => $this->auth->id])->count('id');
		$bookmark_count = model('mall/mallBookmark')->where(['user_id' => $this->auth->id,'status'=>1])->count('id');
		$history_count = model('mall/mallHistory')->where(['user_id' => $this->auth->id,'status'=>1])->count('id');

		$this->count_items = [
			['title' => '订单总数', 'value' => $order_count, 'url' => url('mall/my.order/index')],
			['title' => '收藏商品', 'value' => $bookmark_count, 'url' => url('mall/my.bookmark/index')],
			['title' => '浏览足迹', 'value' => $history_count, 'url' => url('mall/my.history/index')],
			['title' => '我的积分', 'value' => $this->user_info['score']],
		];
	}

    protected function category_tree ($data,$parent = 0)
    {
        //树结构层级数组
        $tree = [];
//        if ($data && is_array($data)) {
        if ($data) {
            foreach ($data as $v) {
                if ($v['pid'] == $parent) {
                    $tree[] = [
                        'id' => $v['id'],
                        'title' => $v['title'],
                        'is_recommend'=>$v['is_recommend'],
                        'pid' => $v['pid'],
                        'children' => $this->category_tree($data, $v['id']),
                    ];
                }
            }
        }
        return $tree;
    }
    protected function category_level($data,$parent,$level = 0)
    {
        //仅实现层级关系
        static $categories = [];
        if ($data && is_array($data)) {
            foreach ($data as $key => &$item) {
                if ($item['pid'] == $parent) {
                    $item['level'] = $level;
//                    $categories[$key][] = $item;
                    $categories[] = $item;
                    $this->category_level ($data,$item['id'],$level+1);
                }
            }
        }
        return $categories;
    }
    protected function getChilIds($category_id)
    {
	    $category_list = model('mall/mallCategory')->field('id,is_recommend,title,pid')->where('pid','in',$category_id)->column('id');
	    $category_list[]=$category_id;

        return $category_list;
    }
    protected function getFeatherIds($category_id)
    {
        //初始化ID数组
        $array[] = $category_id;
        do
        {
            $ids = '';
            $where['id'] = array('in',$category_id);
            $category_list = model('mall/mallCategory')->field('id,pid')->where($where)->select();
            if(!empty($category_list)){
                foreach ($category_list as $v)
                {
                    if($v['pid'] != 0)
                    {
                        $feather_array[] = $v['pid'];
                        $ids .= ',' . $v['pid'];
                    }
                }
                $ids = substr($ids, 1, strlen($ids));
                $category_id = $ids;
            }
        }
        while (!empty($category_list));
        $feather_ids = implode(',', $feather_array);

        return $feather_ids;   // 返回字符串
        //return $feather_array // 返回数组
    }
    protected function getCateColumn($category_id)
    {
        $where['pid'] = array('in',$category_id);
        $array = model('mall/mallCategory')->where($where)->column('id,title');
        return $array;
    }
    protected function assignCategory()
    {
        $map  = ['status' => 1];
        $list = model('mall/mallCategory')->where($map)->select();
        if( ! empty($list)) {
            foreach($list as &$item) {
                $item_map           = ['status' => 1, 'category_id' => $item['id']];
                $goods_list         = model('mall/mallGoods')->where($item_map)->order('num_stock desc,id desc')->limit(8)->select();
                $item['goods_list'] = $goods_list;
            }
        }
        $this->assign('category_list', $list);
    }
    protected function assignOneCategory($id = 0)
    {
        $info = model('mall/mallCategory')->find($id);
        $this->assign('category', $info);

        return $info;
    }
    protected function assignFloor($category,$tempname)
    {

        $where['pid'] = array('in',$category);
        $floor = model('mall/mallCategory')->where('pid','in',$category)->column('id,title');

        $this->assign('floor_'.$tempname,$floor);

	    $cate_child = $category;
//        $cate_child = $this->getChilIds($category);
//	    var_dump('sfdsdf2');die();
        $goods_floor = model('mall/mallGoods')->where('category_id','in',$cate_child)->order('num_stock desc,id desc')->limit(8)->select();
        $this->assign('goods_floor_'.$tempname,$goods_floor);

    }

    protected function assignGoods()
    {
        $map = ['status' => 1];

        $list = model('mall/mallGoods')->where($map)->order('num_stock desc,id desc')->limit(8)->select();
        //        if(!empty($list))
        //        {
        //            foreach ($list as &$item) {
        //                var_dump($item['icon']);
        //                $image = thumb($item['icon'],255,188);
        //                var_dump($item['icon'],$image);
        //            }
        //        }
        $this->assign('goods_list', $list);
    }
    public function addhistory($goods_info)
    {
        $history_indo = model('mall/MallHistory')->where(['user_id'=>$this->auth->id,'goods_id'=>$goods_info['id']])->find();
        if(empty($history_indo))
        {
            $history_data = [
                'user_id'=>$this->auth->id,
                'category_id'=>$goods_info['category_id'],
                'goods_id'=>$goods_info['id'],
                'status'=>1
            ];

            model('mall/MallHistory')->allowField(true)->save($history_data);
        }
    }
    protected function getGoodsColumn($category_id)
    {
        $where['category_id'] = ['in',$category_id];
        $array = model('mall/mallGoods')->select();
        dump($array);die;
        return $array;
    }
    protected function assignOneGood($id = 0)
    {

        $info = model('mall/mallGoods')->find($id);
        if(empty($info)) {
            $this->error('商品不存在');
        }
        if(\yunbao\modelhelp::installed('Vip') && ! empty($info['type_id'])) {
            $vip_info = model('vip/Vip')->find($info['type_id']);

            $info['vip_title'] = $vip_info['title'];
        }

        $this->addhistory($info);

        $this->assign('info', $info);
        return $info;
    }

    protected function assignGoodsOrder($goods_id = 0)
    {
        $map  = ['goods_id' => $goods_id, 'status' => 1];
        $list = model('mall/mallOrder')->where($map)->select();
        if( ! empty($list)) {
            foreach($list as &$item) {
                $user_info    = \yunbao\user::query_user(['nickname', 'id'], $item['user_id']);
                $item['user'] = $user_info;
            }
        }
        $this->assign('goods_order_list', $list);
    }
    protected function assignOrder()
    {
        $map  = ['status' => 1, 'uid' => $this->auth->id];
        $list = model('mall/mallOrder')->where($map)->select();
        $this->assign('order_list', $list);
    }
}